<script setup>
    import {ref} from 'vue'
    import { Plus } from '@element-plus/icons-vue'
    import { ElMessage } from 'element-plus'
     import  axios  from 'axios';
import { useRouter } from 'vue-router';
    const imageUrl = ref('')
    let user=ref({
        name:"",
        password:"",
        img:""
    });
    function beforeAvatarUpload(rawFile){
        if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
    }
    function handleAvatarSuccess(response, uploadFile){
        imageUrl.value = URL.createObjectURL(uploadFile.raw)
        user.value.img=response.data
    }
    let router=useRouter();
    function onSubmit(){
        axios.post("http://localhost:8080/regist",user.value
    ).then((res) => {
    if (res.status == 200) {
      let list = res.data;
      if (list.code == 200) {
        ElMessage({
          message: list.msg,
          type: 'success',
        })
         router.push( "/showview")
      }else{
        ElMessage({
          message: list.msg,
          type: 'Error',
        })
      }
    }
  })
    }
</script>

<template>
<div class="border">
    <el-form :model="user" label-width="auto" style="max-width: 600px">
        <el-form-item label="头像： ">
            <el-upload 
    class="avatar-uploader"
    action="http://localhost:8080/upload"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
    </el-form-item>
    <el-form-item label="用户名： ">
      <el-input v-model="user.name" />
    </el-form-item>
    <el-form-item label="密码： ">
      <el-input v-model="user.password" />
    </el-form-item>
    <el-button type="primary" @click="onSubmit">注册</el-button>
    </el-form>
</div>
</template>

<style scoped>
.border {
    margin-left: 250px;
    width: 600px;
}
:deep(.avatar-uploader .avatar) {
  width: 178px;
  height: 178px;
  display: block;
}

:deep(.avatar-uploader .el-upload) {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

:deep(.avatar-uploader .el-upload:hover ){
  border-color: var(--el-color-primary);
}

:deep(.el-icon.avatar-uploader-icon ){
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
